package cn.iocoder.yudao.module.trade.convert.order;

import cn.iocoder.yudao.module.trade.controller.admin.base.product.property.ProductPropertyValueDetailRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderExcelItemVO;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.StyleProperty;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

public class TradeOrderExcelConvert implements Converter<List> {

    public WriteCellData<?> convertToExcelData(List value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        StyleProperty styleProperty = new StyleProperty();
        styleProperty.setWrapped(true);
        styleProperty.setShrinkToFit(true);
        contentProperty.setContentStyleProperty(styleProperty);
        List<TradeOrderExcelItemVO.Item> items = (List<TradeOrderExcelItemVO.Item>)value;
        return new WriteCellData<>(items.stream().map(this::convert).collect(Collectors.joining("\n")));
    }

    private String convert(TradeOrderExcelItemVO.Item item) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(item.getSpuName());
        stringBuilder.append("【")
                .append(new BigDecimal(item.getPayPrice()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP))
                .append("元")
                .append(" / ")
                .append(item.getCount())
                .append("】");
        for (ProductPropertyValueDetailRespVO property : item.getProperties()) {
            stringBuilder.append("【")
                    .append(property.getPropertyName())
                    .append("：")
                    .append(property.getValueName())
                    .append("】");
        }
        return stringBuilder.toString();
    }

}
